"use strict";
// pages/room/detail.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    user_id: 0,
    show_home: false,
    owner: false,
    info: {},
    relaid:"",
    rolename:"",
  },
  refresh: function () {
    app.api.api_user_detail({ user_id: this.data.user_id }).then(res => {
      this.setData({ info: res })
    })
  },
  input_change:function(e){
    app.api.api_user_find(e.detail).then(res => {
      this.selectComponent("#searchbar").setSelectVals(res);
    })
  },
  selected:function(e){
    this.setData({ relaid: e.detail.id,rolename:e.detail.name })
  },
  
  saverela: function(){
    app.api.api_user_saverela({ user_id: this.data.user_id,relaid:this.data.relaid,
      rela_user_desc:this.data.info.rela_user_desc,}).then(res => {
      wx.showToast({
        title: '已提交',
        icon: 'none'
      })
    })
  },
  deletewechatuser: function () {
    app.api.api_wechatuser_delete({
      user_id: this.data.user_id
    }).then(res => {
      wx.navigateTo({
        url: 'list'
      })
    })
  },
  deleterela:function(){
    app.api.api_user_deleterela({
      user_id: this.data.user_id
    }).then(res => {
      this.refresh()
    })
  },
  bindKeyInput: function (e) {
    this.data[e.currentTarget.dataset.obj] = e.detail.value
  },
  
  edit: function(){
    wx.navigateTo({
      url: 'edit?relaid=' + this.data.relaid
    })
  },
  
  /**
   * 生命周期函数--监听页面加载，设置userid
   */
  onLoad: function (options) {
    this.setData({ show_home: getCurrentPages().length == 1 })
    const scene = decodeURIComponent(options.scene)
    let user_id = '';
    scene.split("&").map(s => {
      if(s.substring(0, s.indexOf("=")) == "user_id"){
        user_id = s.substring(s.indexOf("=") + 1, s.length)
      } 
    })
    let from_scene = false
    if(user_id){
      from_scene = true
    }else{//第一次为空，所以这里设置 rela
      user_id = options.user_id
      this.setData({ relaid: parseInt(options.relaid) })
    }
    if (user_id) {
      this.setData({ user_id: parseInt(user_id) })
      if(from_scene){
        // this.follow()
      }
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.refresh()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    let title = '筱薇课'
    if (this.data.info.name) {
      title += " - " + this.data.info.name
    }
    return {
      title: title,
      page: '/pages/user/detail?user_id=' + this.data.user_id + '&relaid=' + this.data.relaid
    }
  }
})